//
//  HMNavigationViewController.swift
//  Weibo24
//
//  Created by apple on 17/3/26.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit
//  自定义导航视图控制器
class HMNavigationViewController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        //  设置边缘手势的代理
        self.interactivePopGestureRecognizer?.delegate = self
    }
    
    //  监听push操作
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if self.viewControllers.count > 0 {
            //  如果当前的控制的个数等于1,表示根视图控制器
            if self.viewControllers.count == 1 {
                //  获取上一级控制器的标题
                let title = self.viewControllers.first?.title
                //  设置push进来控制器的左侧按钮
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title!,imageName: "navigationbar_back_withtext", target: self, action: #selector(leftAction))
                //  设置视图控制的标题
                viewController.navigationItem.title = "当前控制器显示的是第\(self.viewControllers.count + 1)级控制"
            } else {
                //  不是第二级控制器的导航栏按钮的设置
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回",imageName: "navigationbar_back_withtext", target: self, action: #selector(leftAction))
                //  设置视图控制的标题
                viewController.navigationItem.title = "当前控制器显示的是第\(self.viewControllers.count + 1)级控制"
            }
            //  隐藏tabbar,会有黑影
            //  1. 解决设置window背景色
            //  2. 设置tabbar视图的背景图片
            viewController.hidesBottomBarWhenPushed = true

        }
        
        
        
        print("当前控制器的个数\(self.viewControllers.count)")
        super.pushViewController(viewController, animated: animated)
        print("当前控制器的个数\(self.viewControllers.count)")
    }
    
    //  MARK: ---左侧导航栏按钮的点击
    @objc private func leftAction() {
        //  返回
        self.popViewController(animated: true)
    }

    //  MARK: ---边缘手势的代码方法
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        //  如果是根视图控制器不需要开启边缘手势
        if self.viewControllers.count == 1 {
            return false
        }
        return true
    }
}
